// pagesProperty/pages/attendance/sign/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js")
, common_Js = require("../../../../../component/common.js");
import Dialog from '../../../../../component/vant/dialog/dialog';
import Toast from '../../../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading:true,
    faceUrl:'',
    wd:0,
    jd:0,
    am_begin_duty_time:'',
    am_end_duty_time:'',
    pm_begin_duty_time:'',
    pm_end_duty_time:'',
    remark:'',
    am_begin_duty_time_user:'',
    am_end_duty_time_user:'',
    pm_begin_duty_time_user:'',
    pm_end_duty_time_user:'',
    is_bang_group:0,
    is_band_sys_user: 0,
    sys_user_name: '',
    duty_group_name: '',
    property_name: '',
    estate_name: '',
    nowSignType: 0,
    nowSignTime: '',
    autoTime: null,
    is_in_range: 0,
    is_late_am: "",
    is_late_pm: "",
    isLateState: 0,
    is_check_holiday: 0,
    holiday_check_count: 0,
    leaveTime:0,
    myGroupList:[],
    defaultGroup:{},
    groupNames:[],
    isShowPopupGroup:false,
    fileList1:[],
    is_clear:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  getTime(){
    var today = new Date();//获取当前时间
    var hour = today.getHours();//获取当前小时
    var minutes = today.getMinutes();//获取当前分钟
    var seconds = today.getSeconds();//获取当前秒钟

    var time = (hour < 10 ? '0' + hour : hour) + ":" + (minutes < 10 ? '0' + minutes : minutes) + ":" + (seconds < 10 ?  '0' + seconds : seconds);
    // console.log('time',time)
    this.setData({
      nowSignTime: time
    })
  },
  startTime(){
    var that = this;
    this.autoTime = setInterval(function(){
      that.getTime()
    },1000);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin == 1){
      // var userInfo = app.getStorageSync("userInfo");
      // this.setData({
      //   faceUrl: userInfo.avatarUrl
      // })
      this.modalcnt(0);      
    }else{
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
  },
  modalcnt:function  (is_clear)  {
    var  that  =  this
    that.data.pageIndex = 1;
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          wd : latitude,
          jd : longitude
        })
        app.setStorageSync("lng",longitude);
        app.setStorageSync("lat",latitude);
        that.onHttpInit(is_clear);
      },fail:function(res){
        that.data.wd = 0
        that.data.jd = 0
        that.onHttpInit(is_clear)
      }
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function (is_clear) {
    var that = this
    fetchApi.httpPost('/attendance/view', { 
      lng: that.data.jd,
      lat: that.data.wd,
    }, 
      function (res) {
        console.log('获取考勤打卡界面详情', res)
        if (res.data.code == 0) {
          var is_band_sys_user = res.data.data.is_band_sys_user;
          if(is_band_sys_user == 1){
            that.data.is_in_range = res.data.data.is_in_range;
            that.setData({
              is_band_sys_user: res.data.data.is_band_sys_user,
              sys_user_name: res.data.data.sys_user_name,
              duty_group_name: res.data.data.duty_group_name,
              property_name: res.data.data.property_name,
              estate_name: res.data.data.estate_name,
              is_in_range: res.data.data.is_in_range,
              faceUrl: res.data.data.faceUrl,

              myGroupList: res.data.data.myGroupList,
              defaultGroup: res.data.data.defaultGroup,
              groupNames: res.data.data.groupNames,
  
              am_begin_duty_time: res.data.data.am_begin_duty_time,
              am_end_duty_time: res.data.data.am_end_duty_time,
              pm_begin_duty_time: res.data.data.pm_begin_duty_time,
              pm_end_duty_time: res.data.data.pm_end_duty_time,
              
              
              am_begin_duty_state: res.data.data.am_begin_duty_state,
              am_end_duty_state: res.data.data.am_end_duty_state,
              pm_begin_duty_state: res.data.data.pm_begin_duty_state,
              pm_end_duty_state: res.data.data.pm_end_duty_state,

              am_begin_duty_time_user: res.data.data.am_begin_duty_time_user,
              am_end_duty_time_user: res.data.data.am_end_duty_time_user,
              pm_begin_duty_time_user: res.data.data.pm_begin_duty_time_user,
              pm_end_duty_time_user: res.data.data.pm_end_duty_time_user,
  
              nowSignType : res.data.data.nowSignType,
              nowSignTime : res.data.data.nowSignTime,
              is_late_am: res.data.data.is_late_am,
              is_late_pm: res.data.data.is_late_pm,

              isLateState: res.data.data.isLateState,
              is_check_holiday: res.data.data.is_check_holiday,
              holiday_check_count: res.data.data.holiday_check_count,

              leaveTime: res.data.data.leaveTime,
            })
            
            that.startTime();
          }
        }

        if(is_clear == 1){
          that.setData({
            fileList1: [],
            message:'',
          })
        }
        that.setData({
          isLoading:false
        })
    },function(){})
  },
  goBandUser(){
    wx.navigateTo({
      url: '../band/index',
    })
  },
  goSign(){ 
    var that = this;
    var message = "是否要打外勤？";
    if(this.data.isLateState == 1){
      message = "当前已迟到，是否打迟到卡？";
    }
    if(this.data.is_in_range == 0){
      Dialog.confirm({
        title: '打卡提醒',
        message
      }).then(() => {
        that.onHttpSign(1);
      })
      .catch(() => {
        Dialog.close();
      });
    }else{
      that.onHttpSign(0);
    }
  },
  /**
   * 签到操作
   */
  onHttpSign:function(isOut){
    var that = this;
    if(this.data.is_in_range == 0){
      if(this.data.remark == ''){
        Toast('请输入外勤说明');
        return;
      }

      if(this.data.fileList1.length == 0){
        Toast('请上传图片');
        return;
      }
    }
    
    fetchApi.httpPost('/attendance/sign', {
      lng: that.data.jd,
      lat:that.data.wd,
      address:'',
      remark:that.data.remark,
      isOut:isOut,
      imgUrl:'',
    }, function (res) {
      console.log("解绑人员操作", res)
      if (res.data.code == 0) {

        var record_id = res.data.data;

        that.data.submitState = false;
        that.setData({
          submitState : false,
          estateUserList:[],
          propertyUserList:[],
        })
        Toast.success(res.data.msg);
        if (record_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/attendance/uploadImg',
              filePath: that.data.fileList1[index].url,
              name: 'file',
              formData: {
                record_id: record_id,
                img_type:0,
              },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        that.modalcnt(1);
      }else{
        that.data.submitState = false;
        that.setData({
            submitState : false,
        })
        Toast( res.data.msg);
        return;
      }
    },function(){})
  },
  
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
	  wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  goRecord(){
    wx.navigateTo({
      url: '../reg/index',
    })
  },
  goHoliday(){
    wx.navigateTo({
      url: '../leave/index',
    })
  },
  goCheckHoliday(){
    wx.navigateTo({
      url: '../checkList/index',
    })
  },
  inputHandle(e){
    console.log('inputHandle', e)
    this.data.remark = e.detail;
  },
  handleChangeGroup(){
    this.setData({
      isShowPopupGroup: ! this.data.isShowPopupGroup
    })
  },
  onConfirmChangeGroup(e){
    console.log('onConfirmChangeGroup',e);
    var index = e.detail.index;
    var chooseGroup = this.data.myGroupList[index];
    console.log('chooseGroup',chooseGroup);

    this.onHttpChangeDefaultGroup(chooseGroup.id);
  },
  onHttpChangeDefaultGroup(groupId){
    var that = this;
    fetchApi.httpPost('/attendance/changeDefaultGroup', {
      group_id: groupId,
    }, function (res) {
      console.log("更改默认考勤组", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.handleChangeGroup();
        that.modalcnt(0);        
      }else{
        Toast( res.data.msg);
        return;
      }
    },function(){})
  }
})